//
//  TrackListDetailViewController.m
//  瓜子电台
//
//  Created by lanou on 16/8/9.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "TrackListDetailViewController.h"
#import "TrackDetailTableViewCell.h"
#import "XiaoBianMoreCombineView.h"
#import "TrackIntroTableViewCell.h"
#import "XiaoBianMoreDetailModel.h"
#import "AuthorInfoTableViewCell.h"
#import "AuthorInfoModel.h"
#import "XiaoBianMoreTableViewCell.h"
#import "XiaoBianMoreModel.h"

@interface TrackListDetailViewController ()

@property (nonatomic, strong) UITableView *tableView1;

@property (nonatomic, strong) UITableView *tableView2;

@property (nonatomic, strong) XiaoBianMoreDetailModel *moreDetailModel;

@property (nonatomic, strong) AuthorInfoModel *authorInfoModel;

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TrackListDetailViewController

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"专辑详情";
    [self.tableView registerNib:[UINib nibWithNibName:@"TrackDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_1"];
    [self requestData];
}

- (void)requestData
{
    NSString *str = @"http://mobile.ximalaya.com/mobile/v1/album/detail?albumId=266276&device=iPhone&statEvent=pageview%2Falbum%40266276&statModule=%E5%B0%8F%E7%BC%96%E6%8E%A8%E8%8D%90&statPage=tab%40%E5%8F%91%E7%8E%B0_%E6%8E%A8%E8%8D%90&statPosition=1";
    NSString *detailUrl = [str stringByReplacingOccurrencesOfString:@"albumId=266276" withString:[NSString stringWithFormat:@"albumId=%ld",(long)self.model.albumId]];
    [NetWorkrequestManage requestWithType:GET url:detailUrl parameters:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        self.moreDetailModel = [[XiaoBianMoreDetailModel alloc] init];
        [self.moreDetailModel setValuesForKeysWithDictionary:dict[@"data"][@"detail"]];
        
        self.authorInfoModel = [[AuthorInfoModel alloc] init];
        [self.authorInfoModel setValuesForKeysWithDictionary:dict[@"data"][@"user"]];
        
        for (NSDictionary *dataDic in dict[@"data"][@"recs"][@"list"]) {
            XiaoBianMoreModel *model = [[XiaoBianMoreModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            [self.dataArray addObject:model];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView1 reloadData];
            [self.tableView2 reloadData];
        });
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.tableView) {
        return 2;
    } else if (tableView == self.tableView1) {
        return 3;
    } else {
        return 1;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {
        return 1;
    } else if (tableView == self.tableView1) {
        if (section == 2) {
            return self.dataArray.count;
        } else {
            return 1;
        }
    } else {
        return 1;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            TrackDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1" forIndexPath:indexPath];
            [cell setDataWithModel:self.model];
            return cell;
        } else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_2"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell_2"];
            }
            XiaoBianMoreCombineView *combineView = [[XiaoBianMoreCombineView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 180)];
            self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - 40 - 180 - 64 - 64)];
            [self.tableView1 registerNib:[UINib nibWithNibName:@"TrackIntroTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_3"];
            [self.tableView1 registerNib:[UINib nibWithNibName:@"AuthorInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_4"];
            [self.tableView1 registerNib:[UINib nibWithNibName:@"XiaoBianMoreTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell_5"];
            
            self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 40, kScreenWidth, kScreenHeight - 40 - 180 - 64)];
            self.tableView1.delegate = self;
            self.tableView1.dataSource = self;
            [combineView addSubview:self.tableView1];
            [combineView addSubview:self.tableView2];
            
            [cell addSubview:combineView];
            return cell;
        }
    } else if (tableView == self.tableView1) {
        if (indexPath.section == 0) {
            TrackIntroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_3" forIndexPath:indexPath];
            cell.intro.text = self.moreDetailModel.intro;
            return cell;
        } else if (indexPath.section == 1) {
            AuthorInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_4" forIndexPath:indexPath];
            [cell setDataWithModel:self.authorInfoModel];
            return cell;
        } else {
            XiaoBianMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_5" forIndexPath:indexPath];
            [cell setDataWithModel:self.dataArray[indexPath.row]];
            return cell;
        }
    } else {
        XiaoBianMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_5" forIndexPath:indexPath];
        [cell setDataWithModel:self.dataArray[indexPath.row]];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        if (indexPath.section == 0) {
            return 180;
        } else {
            return kScreenHeight - 180;
        }
    } else if (tableView == self.tableView1) {
        if (indexPath.section == 0) {
            return 125;
        } else if (indexPath.section == 1) {
            return 150;
        } else {
            return 105;
        }
    } else {
        return 105;
    }
    
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
